﻿using OPC.DataBase.BLL;
using OPC.DataBase.DAL;
using OPC.DataBase.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OPC.DataBase.UI
{
    public partial class Form1 : Form
    {
        List<OPCINFO> DataSource_OPC = new List<OPCINFO>();

        OPCInfoService OPCInfoService = new OPCInfoService();
        OPCLogService OPCLogService = new OPCLogService();
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var MyOPCGroup= OPCClient.Start();
            DataSource_OPC = OPCClient.Data.Values.ToList();
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = DataSource_OPC;
            MyOPCGroup.DataChange += MyOPCGroup_DataChange;
        }

        /// <summary>
        /// OPC主事件处理
        /// </summary>
        /// <param name="TransactionID"></param>
        /// <param name="NumItems"></param>
        /// <param name="ClientHandles"></param>
        /// <param name="ItemValues"></param>
        /// <param name="Qualities"></param>
        /// <param name="TimeStamps"></param>
        private void MyOPCGroup_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)
        {
            string itemValue = "";
            int clientHandle = 0;
            DateTime timeStamp = new DateTime();
            for (int i = 1; i < NumItems+1; i++)
            {
                itemValue = ItemValues.GetValue(i).ToString();
                clientHandle = Convert.ToInt32(ClientHandles.GetValue(i));
                timeStamp = (DateTime)(TimeStamps.GetValue(i));


                if (OPCClient.Data[clientHandle].GROUP == "insert")
                {
                    OPCLOG log = new OPCLOG
                    {
                        CLIENTHANDLE = clientHandle,
                        ITEMVALUE = itemValue,
                        CREATEDATE = timeStamp
                    };
                    SQLiteDAL dAL = new SQLiteDAL();
                    dAL.Insert(log);
                }


                OPCClient.Data[clientHandle].ITEMVALUE = itemValue;
                OPCClient.Data[clientHandle].TIMESTAMP = timeStamp;
                OPCInfoService.Change(OPCClient.Data[clientHandle]);

                var obj = DataSource_OPC.Where(o => o.CLIENTHANDLE == clientHandle).FirstOrDefault();
                obj.ITEMVALUE = itemValue;
                obj.TIMESTAMP = timeStamp;

                dataGridView1.DataSource = new List<OPCINFO>();
                dataGridView1.DataSource = DataSource_OPC;
            }
        }

        /// <summary>
        /// 取消按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            OPCClient.DisConnect();
        }

        /// <summary>
        /// 窗口关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            OPCClient.DisConnect();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            OPCClient.Data[1002].ITEM.Write("DELL");
        }
    }
}
